package com.promise.jokerdream.work.penguin

import com.promise.jokerdream.database.entity.ImmortalsGameConfigEntity
import com.promise.jokerdream.config.game.ImmortalType
import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 仙武修真工作
class ImmortalsWork(
    private val config: ImmortalsGameConfigEntity,
    uin: String
): BaseWork(uin) {
    
    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }
    
    override val name: String
        get() = "仙武修真"

    override suspend fun executeWork(): WorkResult {
        return try {
            // 1. 先查询当前寻访进展
            val queryResult = gameApi.queryImmortals()
            queryResult.fold(
                onSuccess = { response ->
                    // 2. 查看任务列表领取奖励
                    for (task in response.task) {
                        if (task.status == 1) { // 已完成但未领取
                            val rewardResult = gameApi.getImmortalsReward(task.id)
                            rewardResult.fold(
                                onSuccess = { rewardResponse ->
                                    historyManager.addHistory(name, rewardResponse.getDescription())
                                },
                                onFailure = { exception ->
                                    historyManager.addHistory(name, exception.message)
                                }
                            )
                        }
                    }
                    
                    // 3. 如果immortalId不为0则挑战
                    if (response.immortalId != 0) {
                        val fightResult = gameApi.fightImmortals()
                        fightResult.fold(
                            onSuccess = { fightResponse ->
                                historyManager.addHistory(name, fightResponse.getDescription())
                            },
                            onFailure = { exception ->
                                historyManager.addHistory(name, exception.message)
                            }
                        )
                    }
                    
                    // 4. 寻访然后挑战，直至leftNum为0
                    var currentResponse = response
                    while (currentResponse.leftNum > 0) {
                        // 根据配置选择山脉进行寻访
                        val immortalType = try { ImmortalType.valueOf(config.selectedImmortalType) } catch (e: Exception) { ImmortalType.RECOMMENDED }
                        val visitResult = gameApi.visitImmortals(immortalType.mountainId)
                        visitResult.fold(
                            onSuccess = { visitResponse ->
                                historyManager.addHistory(name, visitResponse.getDescription())
                                
                                // 挑战
                                val fightResult = gameApi.fightImmortals()
                                fightResult.fold(
                                    onSuccess = { fightResponse ->
                                        historyManager.addHistory(name, fightResponse.getDescription())
                                        
                                        // 重新查询状态
                                        val newQueryResult = gameApi.queryImmortals()
                                        newQueryResult.fold(
                                            onSuccess = { newResponse ->
                                                currentResponse = newResponse
                                            },
                                            onFailure = { exception ->
                                                historyManager.addHistory(name, exception.message)
                                                currentResponse = currentResponse.copy(leftNum = 0) // 出错时退出循环
                                            }
                                        )
                                    },
                                    onFailure = { exception ->
                                        historyManager.addHistory(name, exception.message)
                                        currentResponse = currentResponse.copy(leftNum = 0) // 出错时退出循环
                                    }
                                )
                            },
                            onFailure = { exception ->
                                historyManager.addHistory(name, exception.message)
                                currentResponse = currentResponse.copy(leftNum = 0) // 出错时退出循环
                            }
                        )
                    }
                    
                    WorkResult.Success
                },
                onFailure = { exception ->
                    historyManager.addHistory(name, exception.message)
                    WorkResult.Failure(exception, exception.message)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }
}